﻿using System;
using System.Collections.Generic;
using System.Text;

namespace USERS.CYT369.COM.DAL
{
    public class RedisService
    {
        protected static readonly RedisService m_Instance = new RedisService();

        /// <summary>
        /// 连接redis数据库
        /// </summary>
        /// <returns></returns>
        private StackExchange.Redis.ConnectionMultiplexer GetConnection()
        {
            var connection = StackExchange.Redis.ConnectionMultiplexer.Connect(CYT369.COM.UTILITY.SystemSettingService.Instance.RedisDB);

            return connection;
        }

        /// <summary>
        /// 单实例
        /// </summary>
        public static RedisService Instance
        {
            get { return m_Instance; }
        }

        /// <summary>
        /// 设置验证码
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="captchCode"></param>
        /// <returns></returns>
        public bool SetMobileCaptcha(string phoneNumber, string captchCode)
        {
            using (var connection = GetConnection())
            {
                var db = connection.GetDatabase(0);
                var r = db.StringSet(phoneNumber, captchCode, new TimeSpan(0, 0, CYT369.COM.UTILITY.SystemSettingService.Instance.CaptchaTimeout));

                return r;
            }
        }

        /// <summary>
        /// 读取验证码
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public string GetMobileCaptcha(string phoneNumber)
        {
            using (var connection = GetConnection())
            {
                var db = connection.GetDatabase(0);
                var r = db.StringGet(phoneNumber);

                return r;
            }
        }

        public bool SetImageCaptcha(string phoneNumber, string captchCode)
        {
            using (var connection = GetConnection())
            {
                var db = connection.GetDatabase(0);
                var r = db.StringSet(phoneNumber, captchCode, new TimeSpan(0, 0, CYT369.COM.UTILITY.SystemSettingService.Instance.CaptchaTimeout));

                return r;
            }
        }

        public string GetImageCaptcha(string phoneNumber)
        {
            using (var connection = GetConnection())
            {
                var db = connection.GetDatabase(0);
                var r = db.StringGet(phoneNumber);

                return r;
            }
        }


    }
}
